<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\admin\Logic;
use app\admin\model\BottleExetLog;
use app\admin\model\BottleLog;
use app\common\server\UrlServer;
use think\Db;


class BottleExetLogLogic{
    public static function lists($get){
        $where = [];
        //启用状态搜索
        if (isset($get['site_id']) && $get['site_id']) {
            $where[] = ['bootle_site_id', '=' , $get['site_id']];
        }

        //启用日期
        if (isset($get['start_time']) && $get['start_time'] != '') {
            $where[] = ['create_time', '>=', $get['start_time']];
        }
        if (isset($get['end_time']) && $get['end_time'] != '') {
            $where[] = ['create_time', '<=', $get['end_time']];
        }
        $bottle = new BottleExetLog();

        $bottle_count = $bottle
            ->where($where)
            ->count();

        $field = 'a.*,b.name as base_name';
        $bottle_list = $bottle
            ->alias('a')
            ->where($where)
            ->field($field)
            ->leftJoin('bottle_site b','a.bootle_site_id=b.id')
            ->page($get['page']??1, $get['limit']??10)
            ->order('id desc')
            ->select()->each(function($v,$index){
                $bottleLog = Db::name('bottle_log')->distinct('bottle_code')->whereIn('id',json_decode($v['bottle_log_id'],true))->select();
                // 解析bottle_code
                $v['bottle_log'] =   $bottleLog ?? [];
                $v['admin_id'] = $bottleLog[0]['admin_id']??'';
                $v['order_id'] = $bottleLog[0]['order_id']??'';
                $v['dname'] =  (!empty($bottleLog[0]['admin_id'])) ? Db::name('driver')->where(['id' => $bottleLog[0]['admin_id']])->value('dname') :'';
//                $v['create_time'] = $bottleLog[0]['create_time'];
                $v['info'] = $bottleLog[0]['info']??'';
                $v['site_id'] = $v['bootle_site_id'];
                $v['type'] = $bottleLog[0]['type']??'';
                $v['type_name'] = '';
                if(!empty($bottleLog[0]['type'])){
                    $v['type_name'] = \app\driver\model\Driver::getTypeText($bottleLog[0]['type'])??'';
                }
                return $v;
            });
//        foreach($bottle_list as $key=>$val){
//            $logIdArr = json_decode($val['bottle_log_id'],true);
//            // 获取所有的编号
//            $bottleLog = BottleLog::where(
//                [['id','in',$logIdArr]]
//            )->all()->toArray();
//            $bottle_list[$key]['bottle_log'] = $bottleLog;
//            $bottle_list[$key]['admin_id'] = ($bottleLog!=false)?$bottleLog[0]['admin_id']:'';
//            $bottle_list[$key]['order_id'] = ($bottleLog!=false)?$bottleLog[0]['order_id']:'';
//            $bottle_list[$key]['type'] = ($bottleLog!=false)?$bottleLog[0]['type']:'';
//            $bottle_list[$key]['info'] = ($bottleLog!=false)?$bottleLog[0]['info']:'';
//
//        }
        return ['count' => $bottle_count, 'list' => $bottle_list];

    }

    //添加钢瓶
    public static function add($post){
        $bottle = new BottleExetLog();
        //拼接数据
        $data = [
            'site_id'     => $post['site_id'],
            'code'        => $post['code'],
            'shared_code' => $post['shared_code'],
            'remark'      => $post['remark'],
            'status'      => 2,//钢瓶默认状态为空瓶
            'use_status'  => 1,//默认启用
            'createtime'  => date("Y-m-d H:i:s",time()),
        ];
        return $bottle->allowField(true)->save($data);
    }

    public static function edit($post){
        $bottle = new BottleExetLog();
        //拼接数据
//        $update_data = [
//            'id'          => $post['id'],
//            'site_id'     => $post['site_id'],
//            'code'        => $post['code'],
//            'shared_code' => $post['shared_code'],
//            'remark'      => $post['remark'],
//            'status'      => $post['status'],//钢瓶默认状态为空瓶
//            'use_status'  => $post['use_status'],//默认启用
//        ];
        $res = $bottle->save($post,['id'=>$post['id']]);
        if($res==false){
            return false;
        }
        return true;
    }

    public static function first($id,$get_data = false){
        $user =  BottleExetLog::get($id);
        return $user;
    }



    /*
     * 删除优惠券
     */
    public static function del($id){
        Db::startTrans();
        try{
            $coupon = new BottleExetLog();
            $coupon->save(['del'=>1,'update_time'=>time()],['id'=>$id]);

            Db::commit();
            return true;
        }catch(\Exception $e) {
            Db::rollback();
            return false;
        }
    }

}